<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      python/Django框架/3-Web框架 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>python/Django框架/3-Web框架</h2>



  <p class="post-date">2020-09-24</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h2 id="一-web框架"><a href="#一-web框架" class="headerlink" title="一 web框架"></a>一 web框架</h2><p>Web框架（Web  framework）是一种开发框架，用来支持动态网站、网络应用和网络服务的开发。这大多数的web框架提供了一套开发和部署网站的方式，也为web行为提供了一套通用的方法。web框架已经实现了很多功能，开发人员使用框架提供的方法并且完成自己的业务逻辑，就能快速开发web应用了。浏览器和服务器的是基于HTTP协议进行通信的。也可以说web框架就是在以上十几行代码基础张扩展出来的，有很多简单方便使用的方法，大大提高了开发的效率。</p>
<h3 id="1-1-wsgiref模块"><a href="#1-1-wsgiref模块" class="headerlink" title="1.1 wsgiref模块"></a>1.1 wsgiref模块</h3><p>最简单的Web应用就是先把HTML用文件保存好，用一个现成的HTTP服务器软件，接收用户请求，从文件中读取HTML，返回。</p>
<p>如果要动态生成HTML，就需要把上述步骤自己来实现。不过，接受HTTP请求、解析HTTP请求、发送HTTP响应都是苦力活，如果我们自己来写这些底层代码，还没开始写动态HTML呢，就得花个把月去读HTTP规范。</p>
<p>正确的做法是底层代码由专门的服务器软件实现，我们用Python专注于生成HTML文档。因为我们不希望接触到TCP连接、HTTP原始请求和响应格式，所以，需要一个统一的接口协议来实现这样的服务器软件，让我们专心用Python编写Web业务。这个接口就是WSGI：Web Server Gateway Interface。而wsgiref模块就是python基于wsgi协议开发的服务模块。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> wsgiref.simple_server <span class="keyword">import</span> make_server</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">mya</span><span class="params">(environ, start_response)</span>:</span></span><br><span class="line">    print(environ)</span><br><span class="line">    start_response(<span class="string">'200 OK'</span>, [(<span class="string">'Content-Type'</span>, <span class="string">'text/html'</span>)])</span><br><span class="line">    <span class="keyword">if</span> environ.get(<span class="string">'PATH_INFO'</span>) == <span class="string">'/index'</span>:</span><br><span class="line">        <span class="keyword">with</span> open(<span class="string">'index.html'</span>,<span class="string">'rb'</span>) <span class="keyword">as</span> f:</span><br><span class="line">            data=f.read()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">elif</span> environ.get(<span class="string">'PATH_INFO'</span>) == <span class="string">'/login'</span>:</span><br><span class="line">        <span class="keyword">with</span> open(<span class="string">'login.html'</span>, <span class="string">'rb'</span>) <span class="keyword">as</span> f:</span><br><span class="line">            data = f.read()</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        data=<span class="string">b'&lt;h1&gt;Hello, web!&lt;/h1&gt;'</span></span><br><span class="line">    <span class="keyword">return</span> [data]</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    myserver = make_server(<span class="string">''</span>, <span class="number">8011</span>, mya)</span><br><span class="line">    print(<span class="string">'监听8010'</span>)</span><br><span class="line">    myserver.serve_forever()</span><br></pre></td></tr></table></figure>

<h2 id="二-手撸自己的Web框架"><a href="#二-手撸自己的Web框架" class="headerlink" title="二 手撸自己的Web框架"></a>二 手撸自己的Web框架</h2><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gj1vegl3a2j308q088q2w.jpg" alt="img"></p>
<h3 id="2-1-models-py"><a href="#2-1-models-py" class="headerlink" title="2.1 models.py"></a>2.1 models.py</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">链接数据库返回字典格式：</span></span><br><span class="line"><span class="string">cursor=pymysql.cursors.DictCursor</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> pymysql</span><br><span class="line"><span class="comment">#连接数据库</span></span><br><span class="line">conn = pymysql.connect(host=<span class="string">'127.0.0.1'</span>,port= <span class="number">3306</span>,user = <span class="string">'root'</span>,passwd=<span class="string">'123456'</span>,db=<span class="string">'web'</span>) <span class="comment">#db：库名</span></span><br><span class="line"><span class="comment">#创建游标</span></span><br><span class="line">cur = conn.cursor()</span><br><span class="line"></span><br><span class="line">sql=<span class="string">'''</span></span><br><span class="line"><span class="string">create table userinfo(</span></span><br><span class="line"><span class="string">        id INT PRIMARY KEY ,</span></span><br><span class="line"><span class="string">        name VARCHAR(32) ,</span></span><br><span class="line"><span class="string">        password VARCHAR(32)</span></span><br><span class="line"><span class="string">)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line">cur.execute(sql)</span><br><span class="line"></span><br><span class="line"><span class="comment">#提交</span></span><br><span class="line">conn.commit()</span><br><span class="line"><span class="comment">#关闭指针对象</span></span><br><span class="line">cur.close()</span><br><span class="line"><span class="comment">#关闭连接对象</span></span><br><span class="line">conn.close()</span><br></pre></td></tr></table></figure>

<h3 id="2-2-myserver-py"><a href="#2-2-myserver-py" class="headerlink" title="2.2 myserver.py"></a>2.2 myserver.py</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> wsgiref.simple_server <span class="keyword">import</span> make_server</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> urls <span class="keyword">import</span> url_patters</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">mya</span><span class="params">(environ, start_response)</span>:</span></span><br><span class="line">    <span class="comment"># print(environ)</span></span><br><span class="line">    start_response(<span class="string">'200 OK'</span>, [(<span class="string">'Content-Type'</span>, <span class="string">'text/html'</span>)])</span><br><span class="line">    func = <span class="literal">None</span></span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> url_patters:</span><br><span class="line">        <span class="keyword">if</span> item[<span class="number">0</span>] == environ.get(<span class="string">'PATH_INFO'</span>):</span><br><span class="line">            func = item[<span class="number">1</span>]</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">    <span class="keyword">if</span> func:</span><br><span class="line">        data = func(environ)</span><br><span class="line">        <span class="keyword">return</span> [data]</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> [<span class="string">b'404'</span>]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    myserver = make_server(<span class="string">''</span>, <span class="number">8011</span>, mya)</span><br><span class="line">    print(<span class="string">'监听8010'</span>)</span><br><span class="line">    myserver.serve_forever()</span><br></pre></td></tr></table></figure>

<h3 id="2-3-urls-py"><a href="#2-3-urls-py" class="headerlink" title="2.3 urls.py"></a>2.3 urls.py</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> views <span class="keyword">import</span> *</span><br><span class="line">url_patters = [</span><br><span class="line">    (<span class="string">'/login'</span>, login),</span><br><span class="line">    (<span class="string">'/index'</span>, index),</span><br><span class="line">    (<span class="string">'/time'</span>, time),</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h3 id="2-4-views-py"><a href="#2-4-views-py" class="headerlink" title="2.4 views.py"></a>2.4 views.py</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">(environ)</span>:</span></span><br><span class="line">    <span class="keyword">with</span> open(<span class="string">'templates/index.html'</span>, <span class="string">'rb'</span>) <span class="keyword">as</span> f:</span><br><span class="line">        data = f.read()</span><br><span class="line">    <span class="keyword">return</span> data</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">time</span><span class="params">(environ)</span>:</span></span><br><span class="line">    <span class="keyword">import</span> datetime</span><br><span class="line">    now=datetime.datetime.now().strftime(<span class="string">'%y-%m-%d %X'</span>)</span><br><span class="line">    print(now)</span><br><span class="line">    <span class="keyword">return</span> now.encode(<span class="string">'utf-8'</span>)</span><br><span class="line"><span class="keyword">from</span> urllib.parse <span class="keyword">import</span> parse_qs</span><br><span class="line"><span class="keyword">import</span> pymysql</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span><span class="params">(request)</span>:</span></span><br><span class="line">    <span class="keyword">if</span> request.get(<span class="string">"REQUEST_METHOD"</span>) == <span class="string">"POST"</span>:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            request_body_size = int(request.get(<span class="string">'CONTENT_LENGTH'</span>, <span class="number">0</span>))</span><br><span class="line">        <span class="keyword">except</span> (ValueError):</span><br><span class="line">            request_body_size = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">        request_body = request[<span class="string">'wsgi.input'</span>].read(request_body_size)</span><br><span class="line">        data = parse_qs(request_body)</span><br><span class="line"></span><br><span class="line">        user = data.get(<span class="string">b"user"</span>)[<span class="number">0</span>].decode(<span class="string">"utf8"</span>)</span><br><span class="line">        pwd = data.get(<span class="string">b"pwd"</span>)[<span class="number">0</span>].decode(<span class="string">"utf8"</span>)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 连接数据库</span></span><br><span class="line">        conn = pymysql.connect(host=<span class="string">'127.0.0.1'</span>, port=<span class="number">3306</span>, user=<span class="string">'root'</span>, passwd=<span class="string">'123456'</span>, db=<span class="string">'web'</span>)  <span class="comment"># db：库名</span></span><br><span class="line">        <span class="comment"># 创建游标</span></span><br><span class="line">        cur = conn.cursor()</span><br><span class="line">        SQL = <span class="string">"select * from userinfo WHERE NAME ='%s' AND PASSWORD ='%s'"</span> % (user, pwd)</span><br><span class="line">        cur.execute(SQL)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> cur.fetchone():</span><br><span class="line"></span><br><span class="line">            f = open(<span class="string">"templates/backend.html"</span>, <span class="string">"rb"</span>)</span><br><span class="line">            data = f.read()</span><br><span class="line">            data = data.decode(<span class="string">"utf8"</span>)</span><br><span class="line">            <span class="keyword">return</span> data.encode(<span class="string">"utf8"</span>)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            print(<span class="string">"OK456"</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="string">b"user or pwd is wrong"</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        f = open(<span class="string">"templates/login.html"</span>, <span class="string">"rb"</span>)</span><br><span class="line">        data = f.read()</span><br><span class="line">        f.close()</span><br><span class="line">        <span class="keyword">return</span> data</span><br></pre></td></tr></table></figure>

<h3 id="2-5-tempaltes下backend-html"><a href="#2-5-tempaltes下backend-html" class="headerlink" title="2.5 tempaltes下backend.html"></a>2.5 tempaltes下backend.html</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">"en"</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"UTF-8"</span>&gt;</span><br><span class="line">    &lt;title&gt;Title&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">登录成功</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>



<h3 id="2-5-tempaltes下index-html"><a href="#2-5-tempaltes下index-html" class="headerlink" title="2.5 tempaltes下index.html"></a>2.5 tempaltes下index.html</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">"en"</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"UTF-8"</span>&gt;</span><br><span class="line">    &lt;title&gt;Title&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">index</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<h3 id="2-5-tempaltes下login-html"><a href="#2-5-tempaltes下login-html" class="headerlink" title="2.5 tempaltes下login.html"></a>2.5 tempaltes下login.html</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">"en"</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"UTF-8"</span>&gt;</span><br><span class="line">    &lt;title&gt;Title&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line"></span><br><span class="line">&lt;h4&gt;登录页面&lt;/h4&gt;</span><br><span class="line">&lt;form action=<span class="string">"http://127.0.0.1:8011/login"</span> method=<span class="string">"post"</span>&gt;</span><br><span class="line">     用户名 &lt;input type=<span class="string">"text"</span> name=<span class="string">"user"</span>&gt;</span><br><span class="line">     密码 &lt;input type=<span class="string">"text"</span> name=<span class="string">"pwd"</span>&gt;</span><br><span class="line">    &lt;input type=<span class="string">"submit"</span>&gt;</span><br><span class="line">&lt;/form&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<h2 id="三-其它socket服务端"><a href="#三-其它socket服务端" class="headerlink" title="三 其它socket服务端"></a>三 其它socket服务端</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'cgi'</span>: CGIServer,</span><br><span class="line"><span class="string">'flup'</span>: FlupFCGIServer,</span><br><span class="line"><span class="string">'wsgiref'</span>: WSGIRefServer,</span><br><span class="line"><span class="string">'waitress'</span>: WaitressServer,</span><br><span class="line"><span class="string">'cherrypy'</span>: CherryPyServer,</span><br><span class="line"><span class="string">'paste'</span>: PasteServer,</span><br><span class="line"><span class="string">'fapws3'</span>: FapwsServer,</span><br><span class="line"><span class="string">'tornado'</span>: TornadoServer,</span><br><span class="line"><span class="string">'gae'</span>: AppEngineServer,</span><br><span class="line"><span class="string">'twisted'</span>: TwistedServer,</span><br><span class="line"><span class="string">'diesel'</span>: DieselServer,</span><br><span class="line"><span class="string">'meinheld'</span>: MeinheldServer,</span><br><span class="line"><span class="string">'gunicorn'</span>: GunicornServer,</span><br><span class="line"><span class="string">'eventlet'</span>: EventletServer,</span><br><span class="line"><span class="string">'gevent'</span>: GeventServer,</span><br><span class="line"><span class="string">'geventSocketIO'</span>:GeventSocketIOServer,</span><br><span class="line"><span class="string">'rocket'</span>: RocketServer,</span><br><span class="line"><span class="string">'bjoern'</span> : BjoernServer,</span><br><span class="line"><span class="string">'auto'</span>: AutoServer,</span><br></pre></td></tr></table></figure>

<p> 都遵循一个协议wsgi（Web Server Gateway Interface web服务网关接口）</p>
</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/python/Django%E6%A1%86%E6%9E%B6/4-Django%E7%AE%80%E4%BB%8B/">
        <span class="nav-arrow">← </span>
        
          python/Django框架/4-Django简介
        
      </a>
    
    
      <a class="nav-right" href="/python/Django%E6%A1%86%E6%9E%B6/22-Django%E9%AB%98%E7%BA%A7%E4%B9%8B-%E8%B7%A8%E5%9F%9F%E9%97%AE%E9%A2%98/">
        
          python/Django框架/22-Django高级之-跨域问题
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#一-web框架"><span class="toc-nav-text">一 web框架</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-1-wsgiref模块"><span class="toc-nav-text">1.1 wsgiref模块</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#二-手撸自己的Web框架"><span class="toc-nav-text">二 手撸自己的Web框架</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-1-models-py"><span class="toc-nav-text">2.1 models.py</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-2-myserver-py"><span class="toc-nav-text">2.2 myserver.py</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-3-urls-py"><span class="toc-nav-text">2.3 urls.py</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-4-views-py"><span class="toc-nav-text">2.4 views.py</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-5-tempaltes下backend-html"><span class="toc-nav-text">2.5 tempaltes下backend.html</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-5-tempaltes下index-html"><span class="toc-nav-text">2.5 tempaltes下index.html</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-5-tempaltes下login-html"><span class="toc-nav-text">2.5 tempaltes下login.html</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#三-其它socket服务端"><span class="toc-nav-text">三 其它socket服务端</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/python/Django框架/3-Web框架/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "python/Django框架/3-Web框架",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>